package com.example.myapplication.apter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.AlacarteBean;


import java.util.List;

import takeout.home.ShopdetailsActivity;

public class Alacarteapter extends RecyclerView.Adapter<Alacarteapter.MyHolder> {
    private Context context;
    private List<AlacarteBean.DataBean> list;
  //  private int  num = 0;
    private int  size  = 0;
    private String TAG  = "TAG";


    public Alacarteapter(Context context, List<AlacarteBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    public void getnummoney(){
        double money = 0;
        try {
            for(AlacarteBean.DataBean bean:list){
                money = money + bean.getNum()*bean.getPrice();
            }
            if(context instanceof  ShopdetailsActivity){
                ShopdetailsActivity shopdetailsActivity  = (ShopdetailsActivity)context;
                shopdetailsActivity.getmoney(money);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @NonNull
    @Override
    public Alacarteapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_alacarte, null);
        return new Alacarteapter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Alacarteapter.MyHolder holder, int position) {
        try {

            size = position;

            Glide.with(context).load(TokenBean.IP + list.get(position).getImgUrl()).into(holder.imgUrl);
            holder.name.setText(list.get(position).getName());
            holder.reduceANDadd.setText(String.valueOf(list.get(position).getNum()));
            holder.saleQuantity.setText("月销售: " + String.valueOf(list.get(position).getSaleQuantity()));
            holder.favorRate.setText("好评率: " + String.valueOf(list.get(position).getFavorRate()));
            holder.price.setText("价格: " + String.valueOf(list.get(position).getPrice()));


            holder.reduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.get(position).getNum()>0) {
                        list.get(position).setNum(list.get(position).getNum()-1);
                        Alacarteapter.this.notifyDataSetChanged();
                        getnummoney();
                    } else {
                        Toast.makeText(context, "输入的数量必须大于0", Toast.LENGTH_SHORT).show();
                    }
                }
            });

            holder.add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.get(position).setNum(list.get(position).getNum()+1);
                    Alacarteapter.this.notifyDataSetChanged();
                    getnummoney();
                }
            });

//            holder.reduceANDadd.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//                }
//
//                @Override
//                public void afterTextChanged(Editable s) {
//                    try {
//                        String s1 = s.toString();
//                        list.get(position).setNum(Integer.parseInt(s1));
//                        Alacarteapter.this.notifyDataSetChanged();
//                        getnummoney();
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            });



        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView imgUrl;
        TextView price;
        TextView favorRate;
        TextView name;
        RelativeLayout re;
        TextView saleQuantity;
        private final EditText reduceANDadd;
        private final TextView reduce;
        private final TextView add;


        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgUrl = itemView.findViewById(R.id.imgUrl);
            name = itemView.findViewById(R.id.name);
            saleQuantity = itemView.findViewById(R.id.saleQuantity);
            favorRate = itemView.findViewById(R.id.favorRate);
            price = itemView.findViewById(R.id.price);
            re = itemView.findViewById(R.id.re);

            reduce = itemView.findViewById(R.id.reduce);
            reduceANDadd = itemView.findViewById(R.id.reduceANDadd);
            add = itemView.findViewById(R.id.add);
        }
    }
}



